
const log = console.log.bind(console)

const json = function(o) {
    const circularReplacer = () => {
        const seen = new WeakSet()
        return (key, value) => {
            if (typeof value === "object" && value !== null) {
                if (seen.has(value)) {
                    return `$ref`
                }
                seen.add(value)
            }
            return value
        }
    }
    return JSON.stringify(o, circularReplacer(), 2)
}

const len = o => o.length

const isArray = o => Array.isArray(o)

const isObject = o => Object.prototype.toString.call(o) === '[object Object]'

const equals = (a, b) => {
    // equals 函数自行实现
    if (isArray(a) && isArray(b)) {
        if (a.length !== b.length) {
            return false
        } else {
            for (let i = 0; i < a.length; i++) {
                const x = a[i]
                const y = b[i]
                if (!equals(x, y)) {
                    return false
                }
            }
            return true
        }
    } else if (isObject(a) && isObject(b)) {
        let keys_a = Object.keys(a)
        let keys_b = Object.keys(b)
        if (keys_a.length !== keys_b.length) {
            return false
        } else {
            for (let i = 0; i < keys_a.length; i++) {
                const key = keys_a[i]
                const value_a = a[key]
                const value_b = b[key]
                if (!equals(value_a, value_b)) {
                    return false
                }
            }
            return true
        }
    } else {
        return (a === b)
    }
}

const ensure = (condition, message) => {
    if (!condition) {
        log(`*** 测试失败, ${message}`)
    } else {
        log('+++ 测试成功')
    }
}

const removeChildClassName = (ele, name) => {
    let childs = ele.childNodes
    for (let i = 0; i < childs.length; i++) {
        const child = childs[i]
        if (child.classList && child.classList.contains(name)) {
            child.classList.remove(name)
        }
    }
}

const addChildClassName = (ele, name) => {
    let childs = ele.childNodes
    for (let i = 0; i < childs.length; i++) {
        const child = childs[i]
        if (child.classList && child.classList.contains(name)) {
            child.classList.add(name)
        }
    }
}

const ajax = (method, path, headers, data, responseCallback) => {
    // let r = new XMLHttpRequest()
    let r = new window.XMLHttpRequest()
    r.open(method, path, true)
    // 这个是套路
    r.setRequestHeader('Content-Type', 'application/json')
    r.onreadystatechange = () => {
        if (r.readyState === 4) {
            // log('r', r.response)
            responseCallback(r)
        }
    }
    // 把对象转成字符串
    data = JSON.stringify(data)
    r.send(data)
}

const showCurrentElement = function(ele) {
    let selectEle = es('.console-select')
    selectEle.forEach(item => {
        item.classList.remove('none-show')
        // setAttribute('data', 'hello')
        let id = item.getAttribute('id')
        // log(47, id)
        if (ele !== `#${id}`) {
            item.classList.add('none-show')
        }
    })
}